package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entity.CommonResult;
import com.atguigu.springcloud.entity.Payment;
import com.atguigu.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
public class PaymentController {

    @Value("${server.port}")
    private String serverPort;

    @Resource
    private PaymentService paymentService;

    @GetMapping("/payment/timeout")
    public String getTimeOut(){

        try {
            TimeUnit.SECONDS.sleep(3);
        }catch (Exception e){
            e.printStackTrace();
        }

        return serverPort;
    }

    @GetMapping("/payment/lb")
    public String getPaymentLB(){
        return serverPort;
    }

    @PostMapping(value = "/payment/create")
    public CommonResult create(@RequestBody Payment payment){
        int r = paymentService.create(payment);
        log.info("实例化成功"+r);
        if(r>0){
            return new CommonResult(200, "实例化成功,port:"+serverPort, r);
        }else{
            return new CommonResult(444, "实例化失败", null);
        }
    }

    @GetMapping(value = "/payment/get/{id}")
    public CommonResult create(@PathVariable("id") Long id){
        Payment r = paymentService.getPaymentById(id);
        log.info("实例化成功"+r);
        if(r != null){
            return new CommonResult(200, "查询成功!!!!!,port"+serverPort, r);
        }else{
            return new CommonResult(444, "没有对应记录，查询id"+id, null);
        }
    }
}
